using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web;
using UMC.Security;
using UMC.Web.UI;
using UMC.Data;
using UMC.Net;

namespace UMC.ITME.Activities
{

    [Apiumc("Account", "Staff", Auth = WebAuthType.User, Weight = 1)]
    class SiteStaffActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var user = this.Context.Token.Identity();
            if (user.IsAuthenticated == false)
            {
                this.Context.Send("Login", true);
            }
            var sName = this.AsyncDialog("Name", user.Name);
            var aUser = Data.DataFactory.Instance().User(sName);
            var Model = this.AsyncDialog("Settings", gkey =>
              {
                  if (aUser == null)
                  {
                      var se = UMC.Data.DataFactory.Instance().Session(Utility.Guid(sName, true).Value);
                      foreach (var s in se)
                      {
                          var Value = UMC.Data.JSON.Deserialize<Data.AccessToken>(s.Content.Span);
                          if (String.Equals(Value?.Username, sName))
                          {
                              aUser = new Data.Entities.User { Id = s.user_id, Alias = Value.Alias, Username = Value.Username };
                              break;
                          }
                      }
                  }
                  var account = Security.Account.Create((aUser?.Id ?? user.Id).Value);
                  var dic = UMC.Web.UISection.Create(new UITitle("人员信息"));
                  var name = aUser?.Username;

                  var Discount = new UIHeader.Portrait(Data.WebResource.Instance().ImageResolve(request.Url, aUser?.Id ?? user.Id, "1", 4, Data.Utility.TimeSpan((aUser?.ActiveTime) ?? DateTime.MinValue).ToString()));


                  Discount.Value(aUser?.Alias ?? user.Alias);

                  Discount.Time(aUser?.Signature ?? "未有签名");

                  Discount.Gradient(0x63b359, 0x63b359);

                  var header = new UIHeader();

                  var style = new UIStyle();
                  header.AddPortrait(Discount);
                  header.Put("style", style);

                  style.Name("value").Size(18);
                  dic.UIHeader = header;

                  dic.Title.Style.BgColor(0x63b359);
                  dic.Title.Style.Color(0xfff);
                  
                  var ac = account[Security.Account.EMAIL_ACCOUNT_KEY];
                  if (ac != null && String.IsNullOrEmpty(ac.Name) == false)
                  {
                      name = ac.Name;
                      int c = name.IndexOf('@');
                      if (c > 0)
                      {
                          var cname = name.Substring(0, c);
                          name = name.Substring(0, 2) + "***" + name.Substring(c);
                      }

                      dic.AddCell('\uf199', "邮箱", name, new UIClick(ac.Name) { Key = "mailto" });
                  }
                  else
                  {
                      dic.AddCell('\uf199', "邮箱", "未绑定");
                  }

                  var cui = dic;

                  ac = account[Security.Account.MOBILE_ACCOUNT_KEY];
                  if (ac != null && String.IsNullOrEmpty(ac.Name) == false)
                  {
                      name = ac.Name;
                      if (name.Length > 3)
                      {
                          name = name.Substring(0, 3) + "****" + name.Substring(name.Length - 3);
                      }

                      cui.AddCell('\ue91a', "手机号码", name, UIClick.Tel(ac.Name));
                  }
                  else
                  {
                      cui.AddCell('\ue91a', "手机号码", "未绑定");
                  }
                  var Organize = UMC.Data.DataFactory.Instance().Organizes(new Data.Entities.User { Id = aUser?.Id ?? user.Id });

                  var ui4 = cui.NewSection();

                  ui4.AddCell("所属组织", "");


                  if (Organize.Length > 0)
                  {
                      foreach (var s in Organize)
                      {

                          ui4.Add(UICell.Create("UI", new WebMeta().Put("text", s.Caption).Put("Icon", "\uf0e8")));

                      }
                  }
                  else
                  {
                      ui4.Add("Desc", new UMC.Web.WebMeta().Put("desc", "未加入组织").Put("icon", "\uf0e8"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                      new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                  }

                  var vs = HotCache.Find(new Entities.Cookie { user_id = (aUser?.Id ?? user.Id).Value }, 0, 500, out var _)
                  .OrderByDescending(r => r.Time);

                  if (vs.Count() > 0)
                  {
                      var ui5 = cui.NewSection();
                      ui5.Header.Put("text", "最近使用");

                      foreach (var s in vs)
                      {
                          if (s.IndexValue == 0)
                          {
                              var time = String.Empty;
                              if (s.Time.HasValue)
                              {
                                  time = UMC.Data.Utility.GetDate(s.Time.Value);
                              }
                              else if (s.LoginTime.HasValue)
                              {
                                  time = UMC.Data.Utility.GetDate(Utility.TimeSpan(s.LoginTime.Value));
                              }
                              if (s.Domain.Contains('_'))
                              {
                                  if (s.Domain.EndsWith("_www"))
                                  {
                                      ui5.AddCell(time, s.Domain.Substring(0, s.Domain.Length - 4));
                                  }
                              }
                              else
                              {

                                  ui5.AddCell(time, s.Domain);
                              }
                          }

                      }
                  }

                  var sess = UMC.Data.DataFactory.Instance().Session((aUser?.Id ?? user.Id).Value)
                  .Where(r => String.Equals("Settings", r.ContentType) == false);//.ToArray();
                  if (sess.Count() > 0)
                  {
                      var ui5 = cui.NewSection();
                      ui5.Header.Put("text", "最近登录");

                      foreach (var s in sess)
                      {
                          ui5.AddCell(UMC.Data.Utility.GetDate(s.UpdateTime), s.ContentType.UTF8());
                      }
                  }

                  dic.SendTo(this.Context);

              });



        }
    }
}